package com.androidWAC;

import android.app.Activity;
import android.database.Cursor;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class AndroidWACActivity extends Activity {
	Calendar cal1= new Calendar();
	DBAdapter db = new DBAdapter(this);
	String spinnertext;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button save =(Button)findViewById(R.id.addEvent);
        Spinner spinner =(Spinner) findViewById(R.id.notice);
        ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,
        		R.array.alertTimes, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        
    	db = db.open();
        
        class MyOnItemSelectedListener implements OnItemSelectedListener{

			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
				// TODO Auto-generated method stub
				//Toast.makeText(parent.getContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
				spinnertext=parent.getItemAtPosition(pos).toString();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
        	
        }
        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
        
        
        
        save.setOnClickListener(onSave);
        
        LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
       
        
        LocationListener locationListener = new LocationListener() {
        
        
	        public void onStatusChanged(String provider, int status, Bundle extra){
	        	
	        }
	        
	        public void onProviderEnabled(String provider){
	        	
	        }
	        
	        public void onProviderDisabled(String provider){
	        	
	        }
	        
			public void onLocationChanged(Location arg0) {
				useLocation(arg0);
				
			}

		
        };
    }
    
    //handle gps data
    public void useLocation(Location location)
    {
    	double lat,lon;
    	lat=location.getLatitude();
    	lon=location.getLongitude();
    	
    }
    
    private View.OnClickListener onSave = new View.OnClickListener(){
    	public void onClick(View v) {
    		/*
    		EditText name =(EditText)findViewById(R.id.name);
    		EditText address=(EditText)findViewById(R.id.addr);
    		r.setName(name.getText().toString());
    		r.setAddress(address.getText().toString());
    		*/
    		EditText title=(EditText)findViewById(R.id.title);
    		EditText location=(EditText)findViewById(R.id.location);
    		DatePicker date=(DatePicker)findViewById(R.id.datePicker1);
    		TimePicker start,end;
    		start=(TimePicker)findViewById(R.id.timePicker1);
    		end=(TimePicker)findViewById(R.id.timePicker2);
    		String alert=spinnertext;
    		
    		db.insertEvent(title.toString(), location.toString(), date.getMonth(), 
    				date.getDayOfMonth(), date.getYear(), start.getCurrentHour(), 
    				start.getCurrentMinute(), end.getCurrentHour(), end.getCurrentMinute());
    		
    		Cursor cur = db.getAllEvents();
    		Log.v("rowCount", cur.getCount()+"");
    	}
    };
}